package com.ruoyi.shbData.service.impl;

import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

import com.deepoove.poi.data.PictureRenderData;
import com.ruoyi.common.code.ServiceHttpCode;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.pad.ChartLineDTO;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExportWordUtils;
import com.ruoyi.common.utils.ProcessPictureUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.calculate.BezierCurveUtil;
import com.ruoyi.common.utils.calculate.MotorUtils;
import com.ruoyi.shbData.domain.pad.DataShbModifyReqDTO;
import com.ruoyi.shbData.domain.pad.ShbChartRespDTO;
import com.ruoyi.shbData.domain.pad.ShbTaskRespDTO;
import com.ruoyi.shbData.domain.plat.DataShb;
import com.ruoyi.shbData.domain.report.ShbReportReqDTO;
import com.ruoyi.shbData.mapper.DataShbMapper;
import com.ruoyi.shbData.service.IDataShbService;
import com.ruoyi.task.domain.entity.DataTaskEntity;
import com.ruoyi.task.service.IDataTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


/**
 * 水泵测试结果Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-22
 */
@Service
public class DataShbServiceImpl implements IDataShbService
{
    @Autowired(required = false)
    private DataShbMapper dataShbMapper;

    @Autowired(required = false)
    private IDataTaskService dataTaskService;

    /**
     * 查询水泵测试结果
     * 
     * @param shbId 水泵测试结果主键
     * @return 水泵测试结果
     */
    @Override
    public DataShb selectDataShbByShbId(Long shbId)
    {
        return dataShbMapper.selectDataShbByShbId(shbId);
    }

    /**
     * 查询水泵测试结果
     *
     * @param dataTaskId
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @Override
    public ShbTaskRespDTO getShbDetailTask(Long dataTaskId) throws ExecutionException, InterruptedException {
        Future<DataTaskEntity> taskFuture = dataTaskService.selectDataTask(dataTaskId);
        Future<List<DataShb>> resultListFuture = selectAllByDataTaskId(dataTaskId);
        ShbTaskRespDTO shbTaskRespDTO = new ShbTaskRespDTO();
        boolean isGetDataTaskEntity = false;
        boolean isGetDataShbEntityList = false;

        while (!isGetDataTaskEntity || !isGetDataShbEntityList) {
            Thread.sleep(10);
            if (taskFuture.isDone() || taskFuture.isCancelled()) {
                isGetDataTaskEntity = true;
                if (taskFuture.isDone()) {
                    DataTaskEntity dataTaskEntity = taskFuture.get();
                    shbTaskRespDTO.setDataTaskEntity(dataTaskEntity);
                }
            }
            if (resultListFuture.isDone() || resultListFuture.isCancelled()) {
                isGetDataShbEntityList = true;
                if (resultListFuture.isDone()) {
                    List<DataShb> dataShbEntityList = resultListFuture.get();
                    if (!CollectionUtils.isEmpty(dataShbEntityList)) {
                        shbTaskRespDTO.getResultList().addAll(dataShbEntityList);
                    }

                }
            }
        }
        return shbTaskRespDTO;
    }

    /**
     * 查询水泵测试结果列表
     * 
     * @param dataShb 水泵测试结果
     * @return 水泵测试结果
     */
    @Override
    public List<DataShb> selectDataShbList(DataShb dataShb)
    {
        return dataShbMapper.selectDataShbList(dataShb);
    }

    /**
     * 新增水泵测试结果
     * 
     * @param dataShb 水泵测试结果
     * @return 结果
     */
    @Override
    public int insertDataShb(DataShb dataShb)
    {
        dataShb.setCreateTime(DateUtils.getNowDate());
        return dataShbMapper.insertDataShb(dataShb);
    }

    /**
     * 修改水泵测试结果
     * 
     * @param dataShb 水泵测试结果
     * @return 结果
     */
    @Override
    public int updateDataShb(DataShb dataShb)
    {
        dataShb.setUpdateTime(DateUtils.getNowDate());
        return dataShbMapper.updateDataShb(dataShb);
    }

    /**
     * 批量删除水泵测试结果
     * 
     * @param shbIds 需要删除的水泵测试结果主键
     * @return 结果
     */
    @Override
    public int deleteDataShbByShbIds(Long[] shbIds)
    {
        return dataShbMapper.deleteDataShbByShbIds(shbIds);
    }

    /**
     * 修改测试数据状态变更2
     *
     * @param dataTaskId
     * @param kyjId
     */
    @Override
    public int updateDataShbByHjId(Long dataTaskId, Long kyjId) {
        return dataShbMapper.updateDataShbByShbId(dataTaskId, kyjId);
    }

    @Override
    public int updateDataShbByDataTaskId(Long dataTaskId) {
        //变更任务状态
        try {
            Long[] dataTaskIds = new Long[] {dataTaskId};
            dataTaskService.deleteDataTaskByDataTaskIds(dataTaskIds, "005");
        }catch (Exception e){
            e.printStackTrace();
        }
        //变更所有任务下数据
        return dataShbMapper.updateDataShbByDataTaskId(dataTaskId);
    }

    /**
     * 删除水泵测试结果信息
     * 
     * @param shbId 水泵测试结果主键
     * @return 结果
     */
    @Override
    public int deleteDataShbByShbId(Long shbId)
    {
        return dataShbMapper.deleteDataShbByShbId(shbId);
    }

    /**
     * 根据任务id获取当前水泵测试数据
     *
     * @param dataTaskId
     * @return
     */
    @Override
    public Future<List<DataShb>> selectAllByDataTaskId(Long dataTaskId) {
        return new AsyncResult<>(dataShbMapper.selectAllByDataTaskId(dataTaskId));
    }

    /**
     * 批量新增水泵测试数据
     *
     * @param list
     * @return
     */
    @Override
    public int batchDataShb(List<DataShb> list) {
        return dataShbMapper.batchDataShb(list);
    }

    /**
     * 批量更新水泵测试数据
     *
     * @param list
     * @return
     */
    @Override
    public int updateBatch(List<DataShb> list) {
        return dataShbMapper.updateBatch(list);
    }

    /**
     * 获取泵效图参数
     *
     * @param shbId
     * @return
     */
    @Override
    public ShbChartRespDTO getChartData(Long[] shbId) {
        ShbChartRespDTO shbChartRespDTO = new ShbChartRespDTO();

        if (shbId == null || shbId.length < 3) {
            shbChartRespDTO.setCode(ServiceHttpCode.SHB_CHART_POINT_LESS.getCode());
            shbChartRespDTO.setMsg(ServiceHttpCode.SHB_CHART_POINT_LESS.getMsg());
            return shbChartRespDTO;
        }

        // 查询数据集合
        List<DataShb> shbResults = dataShbMapper.selectAllDataShbByShbIds(shbId);
        // 去掉集合中瞬时流量为空的点
        List<DataShb> dataFilterList = shbResults.stream().filter(item -> item.getShshll() != null).collect(Collectors.toList());
        // 去掉瞬时流量相同的数据点
        Set<DataShb> shbResultSet = new TreeSet<>(Comparator.comparing(DataShb::getShshll));
        shbResultSet.addAll(dataFilterList);
        ArrayList<DataShb> results = new ArrayList<>(shbResultSet);

        if (results.size() < 3) {
            shbChartRespDTO.setCode(ServiceHttpCode.SHB_CHART_VALID_POINT_LESS.getCode());
            shbChartRespDTO.setMsg(ServiceHttpCode.SHB_CHART_VALID_POINT_LESS.getMsg());
            return shbChartRespDTO;
        }

        // 按瞬时流量排序
        List<DataShb> collect = results.stream().sorted(Comparator.comparing(dataShb -> {return Float.parseFloat(dataShb.getShshll());})).collect(Collectors.toList());
        float maxSsll = Float.parseFloat(collect.get(0).getShshll()), minSsll = Float.parseFloat(collect.get(0).getShshll());
        //瞬时流量
        List<Float> ssllList = new ArrayList<>();
        //单耗
        List<Float> dhFList = new ArrayList<>();
        //扬程
        List<Float> ychFList = new ArrayList<>();
        //轴功率
        List<Float> zglFList = new ArrayList<>();
        //水泵效率
        List<Float> sbxlFList = new ArrayList<>();
        for (DataShb item : collect) {
            // 瞬时流量
            float ssllF = Float.parseFloat(item.getShshll());
            maxSsll = Math.max(maxSsll, ssllF);
            minSsll = Math.min(minSsll, ssllF);
            ssllList.add(ssllF);
            // 单耗
            if (StringUtils.isNotEmpty(item.getSbdh())) {
                if(!item.getSbdh().equals("--")) {
                    float dhF = Float.parseFloat(item.getSbdh());
                    dhFList.add(dhF);
                }else {
                    dhFList.add(0.0f);
                }
            }else {
                dhFList.add(0.0f);
            }
            // 扬程
            if (StringUtils.isNotEmpty(item.getSbych())) {
                if(!item.getSbych().equals("--")) {
                    float ychF = Float.parseFloat(item.getSbych());
                    ychFList.add(Math.abs(ychF));
                }else{
                    ychFList.add(0.0f);
                }
            }else {
                ychFList.add(0.0f);
            }
            // 轴功率
            if (StringUtils.isNotEmpty(item.getZhgl())) {
                if(!item.getZhgl().equals("--")) {
                    float zglF = Float.parseFloat(item.getZhgl());
                    zglFList.add(zglF);
                }else{
                    zglFList.add(0.0f);
                }
            }else {
                zglFList.add(0.0f);
            }
            // 水泵效率
            if (StringUtils.isNotEmpty(item.getShbxl())) {
                if(!item.getShbxl().equals("--")) {
                    float sbxlF = Float.parseFloat(item.getShbxl());
                    sbxlFList.add(sbxlF);
                }else{
                    sbxlFList.add(0.0f);
                }
            }else {
                sbxlFList.add(0.0f);
            }
        }

        ChartLineDTO dhLineData = BezierCurveUtil.calPoints(ssllList, dhFList, false);
        ChartLineDTO zglLineData = BezierCurveUtil.calPoints(ssllList, zglFList, false);
        ChartLineDTO ychLineData = BezierCurveUtil.calPoints(ssllList, ychFList,  false);
        ChartLineDTO bxLineData = BezierCurveUtil.calPoints(ssllList, sbxlFList, false);

        shbChartRespDTO.setMaxShshll(maxSsll);
        shbChartRespDTO.setMinShshll(minSsll);
        shbChartRespDTO.setDh(dhLineData);
        shbChartRespDTO.setZhgl(zglLineData);
        shbChartRespDTO.setYch(ychLineData);
        shbChartRespDTO.setBx(bxLineData);
        return shbChartRespDTO;
    }

    /**
     * 调整参数
     *
     * @param dataShbModifyReqDTO
     * @return
     */
    @Override
    public DataShb modify(DataShbModifyReqDTO dataShbModifyReqDTO) {
        DataShb modifyDataShbEntity = modifyShb(dataShbModifyReqDTO);
        // 更新数据
        updateDataShb(modifyDataShbEntity);
        return modifyDataShbEntity;
    }

    /**
     * 导入报告
     *
     * @param shbReportReqDTO
     */
    @Override
    public String dataShbImportReport(ShbReportReqDTO shbReportReqDTO) {
        // 报告模板名称
        String template = shbReportReqDTO.getTempId();
        //获取生成的工况图并转换png格式
        String rootPath = RuoYiConfig.getProfile() + RuoYiConfig.getChartPath() + "/";
        //工况图路径
        String gkImg = System.currentTimeMillis() + "-工况图.png";
        if(StringUtils.isNotEmpty(shbReportReqDTO.getChart())) {
            boolean res = ProcessPictureUtils.convertToPng(shbReportReqDTO.getChart(), rootPath + shbReportReqDTO.getDataTaskEntity().getDataTaskId() + "/" + gkImg);
            String resString = res ? "成功" : "失败";
            System.out.println("工况图导入:" + resString);
        }
        // 曲线图的长宽
        int picW = 450;
        int picH = 250;
        //导入报告中
        Map<String, Object> params = new HashMap<>();
        //基本检测信息
        if(null != shbReportReqDTO.getDataTaskEntity()) {
            params.put("受检单位", shbReportReqDTO.getDataTaskEntity().getShjdw());
            params.put("设备名称", shbReportReqDTO.getDataTaskEntity().getShbbh());
            params.put("主检姓名", shbReportReqDTO.getDataTaskEntity().getZhjxm());
            params.put("测试人员", shbReportReqDTO.getDataTaskEntity().getCshry());
            params.put("检测日期", DateUtils.dateTime(shbReportReqDTO.getDataTaskEntity().getTaskCreateTime()));
        }
        //水泵数据
        if(null != shbReportReqDTO.getDataShbEntity()) {
            //水泵基本参数
            DataShb dataShb = shbReportReqDTO.getDataShbEntity();
            params.put("瞬时流量", dataShb.getShshll());
            params.put("进口压力", dataShb.getJkyl());
            params.put("出口压力", dataShb.getCkyl());
            params.put("水泵效率", dataShb.getShbxl());
            params.put("管路效率", dataShb.getGlxl());
            params.put("水泵单耗", dataShb.getSbdh());
            params.put("水泵扬程", dataShb.getSbych());
            params.put("大气压", dataShb.getDqyEnvir());
            params.put("温度", dataShb.getWdEnvir());
            params.put("湿度", dataShb.getShdEnvir());
            //电机数据
            params.put("AB线电压", dataShb.getAbV());
            params.put("A相电流", dataShb.getAxdl());
            params.put("BC线电压", dataShb.getBcV());
            params.put("B相电流", dataShb.getBxdl());
            params.put("CA线电压", dataShb.getCaV());
            params.put("C相电流", dataShb.getCxdl());
            params.put("平均电压", dataShb.getPjdy());
            params.put("平均电流", dataShb.getPjdl());
            params.put("电机功率", dataShb.getDjgl());
            params.put("电机效率", dataShb.getDjxl());
            params.put("轴功率", dataShb.getZhgl());
            params.put("输出功率", dataShb.getShcgl());
            params.put("功率因数", dataShb.getGlysh());
            params.put("综合效率", dataShb.getZhxl());
            params.put("负载系数", dataShb.getFzxsh());
            params.put("运行状态", dataShb.getYxzht());
        }
        //工况图
        params.put("工况图", new PictureRenderData(picW, picH, rootPath +gkImg));
        String docRootPath = RuoYiConfig.getProfile() + RuoYiConfig.getDocumentPath() + "/";
        String docFileName = System.currentTimeMillis() + "-shuibeng.docx";
        boolean isOk = ExportWordUtils.exportWord(RuoYiConfig.getProfile() + RuoYiConfig.getTemplatePath() + "/" + template, docRootPath, docFileName, params);
        System.out.println("isOK: 生成测试报告" + isOk + " docFileName: " + docFileName);
        return "/profile/document/" + docFileName;
    }

    /**
     * 水泵重新计算
     * @param dataShbModifyReqDTO
     * @return
     */
    private DataShb modifyShb(DataShbModifyReqDTO dataShbModifyReqDTO) {
        // 电机重新计算
        DataShb dataShbEntity = getPower(dataShbModifyReqDTO);

        dataShbEntity.setJkyl(StringUtils.isNotEmpty(dataShbModifyReqDTO.getJkyl()) ? dataShbModifyReqDTO.getJkyl() : dataShbEntity.getJkyl());
        dataShbEntity.setCkyl(StringUtils.isNotEmpty(dataShbModifyReqDTO.getChkyl()) ? dataShbModifyReqDTO.getChkyl() : dataShbEntity.getCkyl());
        dataShbEntity.setShshll(StringUtils.isNotEmpty(dataShbModifyReqDTO.getShshll()) ? dataShbModifyReqDTO.getShshll() : dataShbEntity.getShshll());
        dataShbEntity.setWc(StringUtils.isNotEmpty(dataShbModifyReqDTO.getWencha()) ? dataShbModifyReqDTO.getWencha() : dataShbEntity.getWc());

        float djxlF = Float.parseFloat(dataShbEntity.getDjxl());  // 电机效率
        float cdxlF = Float.parseFloat(dataShbEntity.getChdxl()); // 传动效率
        float djglF = Float.parseFloat(dataShbEntity.getDjgl());  // 电机功率
        float psgdF = Float.parseFloat(dataShbEntity.getPshgd()); // 排水高度
        float xsgdF = Float.parseFloat(dataShbEntity.getXshgd()); // 吸水高度
        float gdnjF = Float.parseFloat(dataShbEntity.getGj());  // 管道内径

        // 测试方法
        boolean isLlf = dataShbEntity.getTestFunction().equals("流量法");

        float shshllF = 0F;  // 瞬时流量
        float wenchaF = 0F; // 温差
        if (isLlf) {
            shshllF = Float.parseFloat(dataShbEntity.getShshll());
        } else {
            wenchaF = Float.parseFloat(dataShbEntity.getWc());
        }

        float jkylF = StringUtils.isNotEmpty(dataShbEntity.getJkyl()) ? Float.parseFloat(dataShbEntity.getJkyl()) : 0F;      // 进口压力
        float chkylF = StringUtils.isNotEmpty(dataShbEntity.getCkyl()) ? Float.parseFloat(dataShbEntity.getCkyl()) : 0F;    // 出口压力
        float bwchF = StringUtils.isNotEmpty(dataShbEntity.getBwch()) ? Float.parseFloat(dataShbEntity.getBwch()) : 0F;      // 表位差
        float shbychF = StringUtils.isNotEmpty(dataShbEntity.getSbych()) ? Float.parseFloat(dataShbEntity.getSbych()) : 0F;  // 水泵扬程

        float ltmdF = StringUtils.isNotEmpty(dataShbEntity.getLtmd()) ? Float.parseFloat(dataShbEntity.getLtmd()) : 1050F;   // 流体密度
        float psnjF = StringUtils.isNotEmpty(dataShbEntity.getPshnj()) ? Float.parseFloat(dataShbEntity.getPshnj()) : 0.1F;  // 排水内径
        float xshnjF = StringUtils.isNotEmpty(dataShbEntity.getXshnj()) ? Float.parseFloat(dataShbEntity.getXshnj()) : 0.1F; // 吸水内径
        float edllF = StringUtils.isNotEmpty(dataShbEntity.getEdll()) ? Float.parseFloat(dataShbEntity.getEdll()) : 0F;      // 额定流量

        //  流体速度
        if (isLlf) {
//            float ltsdF = shshllF / 3600F / (3.1415F * gdnjF * gdnjF / 4000000f);
//            dataShbEntity.setLtsd(String.format("%.4f", ltsdF));
            dataShbEntity.setLtsd(StringUtils.isNotEmpty(dataShbModifyReqDTO.getLtsd()) ? dataShbModifyReqDTO.getLtsd() : dataShbEntity.getLtsd());
        }

        //  水泵扬程
        float sbycF;
        if (isLlf) {
            sbycF = (float) (1.01972f * Math.pow(10, 5) * (chkylF - jkylF) / ltmdF + bwchF + 6.3778f * Math.pow(10, -9) * shshllF * shshllF * (1f / Math.pow(psnjF, 4) - 1f / Math.pow(xshnjF, 4)));
        } else {
            sbycF = (float) (1.01972f * Math.pow(10, 5) * (chkylF - jkylF) / ltmdF + bwchF + 6.3778f * Math.pow(10, -9) * edllF * edllF * (1f / Math.pow(psnjF, 4) - 1f / Math.pow(xshnjF, 4)));
        }
        dataShbEntity.setSbych(String.format("%.2f", sbycF));

        //  轴功率
        float zglF = djglF * djxlF * cdxlF / 10000f;
        dataShbEntity.setZhgl(String.format("%.2f", zglF));

        //  水泵效率 = 0.286f * 流量(m³/h) * 扬程/轴功率   （传感器数据是m³/h）
        float sbxlF = 0F;
        if (isLlf) {
            if (zglF != 0) {
                sbxlF = (float) (2.72407f * Math.pow(10, -6) * ltmdF * shshllF * sbycF / zglF);
            }
        } else {
            float aF = (float) (0.10197f * Math.pow(10, 6) * (chkylF - jkylF) / ltmdF);
            float bF = (float) (0.10197f * Math.pow(10, 6) * (chkylF - jkylF) / ltmdF + 425f * wenchaF);
            sbxlF = aF / bF;
        }
        sbxlF = Math.max(0, sbxlF);
        sbxlF = Math.min(100, sbxlF);
        dataShbEntity.setShbxl(String.format("%.2f", sbxlF  * 100f));

        //  管路效率 = 排水高度 / 扬程
        float glxlF = 0;
        if (sbycF != 0) {
            glxlF = (psgdF + xsgdF) / sbycF * 100f;
            glxlF = Math.max(0, glxlF);
            glxlF = Math.min(100, glxlF);
            dataShbEntity.setGlxl(String.format("%.2f", glxlF));
        }

        // 单耗 = 1 / (3.67 * 水泵效率 * 电机效率 * 传动效率 * 管路效率)
        float dhF;
        if (sbxlF * djxlF * cdxlF * glxlF != 0) {
            dhF = 1 / (3.67f * sbxlF * djxlF * cdxlF * glxlF / 1000000f);
            dataShbEntity.setSbdh(String.format("%.3f", dhF));
        }

        // 瞬时流量
        float ssllF;
        if (!isLlf && shbychF != 0) {
            // ssll = 349.6f * zgl * sbxl / 100 / sbyc;
            ssllF = (367097.832F * zglF * sbxlF / ltmdF / sbycF);
            dataShbEntity.setShshll(String.format("%.2f", ssllF));
        }
        return dataShbEntity;
    }

    /**
     * 计算电机
     */
    private DataShb getPower(DataShbModifyReqDTO dataShbModifyReqDTO) {
        //根据测试项id获取详情
        DataShb dataShb = dataShbMapper.selectDataShbByShbId(dataShbModifyReqDTO.getShbId());
        DecimalFormat df2 = new DecimalFormat("0.00");;
        //A相电流
        float aA = dataShb.getAxdl().isEmpty() ? 0f : Float.parseFloat(dataShb.getAxdl());
        //B相电流
        float bA = dataShb.getBxdl().isEmpty() ? 0f : Float.parseFloat(dataShb.getBxdl());
        //C相电流
        float cA = dataShb.getCxdl().isEmpty() ? 0f : Float.parseFloat(dataShb.getCxdl());
        //AB线电压
        float abV = dataShb.getAbV().isEmpty() ? 0f : Float.parseFloat(dataShb.getAbV());
        //CA线电压
        float caV = dataShb.getCaV().isEmpty() ? 0f : Float.parseFloat(dataShb.getCaV());
        //BC线电压
        float bcV = dataShb.getBcV().isEmpty() ? 0f : Float.parseFloat(dataShb.getBcV());
        //平均电流
        float pjdl = dataShb.getPjdl().isEmpty() ? 0f : Float.parseFloat(dataShb.getPjdl());
        //平均电压
        float pjdy = dataShb.getPjdy().isEmpty() ? 0f : Float.parseFloat(dataShb.getPjdy());
        //查看变更的参数
        if(StringUtils.isNotEmpty(dataShbModifyReqDTO.getPjdl())) {
            //变更平均电流
            float diffDL = Float.parseFloat(dataShbModifyReqDTO.getPjdl()) - pjdl;
            dataShb.setAxdl(String.format("%.2f", aA + diffDL));
            dataShb.setBxdl(String.format("%.2f", bA + diffDL));
            dataShb.setCxdl(String.format("%.2f", cA + diffDL));
            dataShb.setPjdl(dataShbModifyReqDTO.getPjdl());
        }
        if(StringUtils.isNotEmpty(dataShbModifyReqDTO.getPjdy())) {
            //变更平均电压
            float diffDY = Float.parseFloat(dataShbModifyReqDTO.getPjdy()) - pjdy;
            dataShb.setAbV(String.valueOf(abV + diffDY));
            dataShb.setBcV(String.valueOf(bcV + diffDY));
            dataShb.setCaV(String.valueOf(caV + diffDY));
            dataShb.setPjdy(dataShbModifyReqDTO.getPjdy());
        }
        if(StringUtils.isNotEmpty(dataShbModifyReqDTO.getGlysh())) {
            //变更功率因数
            dataShb.setGlysh(dataShbModifyReqDTO.getGlysh());
        }
        // 如果功率因数为0 ，则赋值为1 ，否则无法计算
        if (dataShb.getGlysh() == null || dataShb.getGlysh().isEmpty() || Float.parseFloat(dataShb.getGlysh()) == 0) {
            dataShb.setGlysh(1 + "");
        }
        float szgl = ((Float.parseFloat(dataShb.getAxdl()) * Float.parseFloat(dataShb.getAbV())) + (Float.parseFloat(dataShb.getBxdl()) * Float.parseFloat(dataShb.getBcV())) + (Float.parseFloat(dataShb.getCxdl()) * Float.parseFloat(dataShb.getCaV()))) / 1000;
        //计算公式是通过平均电流乘以平均电压乘以3
        //float szgl = (Float.parseFloat(dataShb.getPjdl()) * Float.parseFloat(dataShb.getPjdy())) * 3 /1000;
        float djgl = (szgl * Float.parseFloat(dataShb.getGlysh())) / 1.732F;
        dataShb.setDjgl(String.valueOf(df2.format(djgl)));
        MotorUtils motorUtils = new MotorUtils(dataShb.getAbV(), dataShb.getBcV(), dataShb.getCaV(), dataShb.getAxdl(), dataShb.getBxdl(), dataShb.getCdxl(), dataShb.getPjdy(), dataShb.getPjdl());
        Map<String, String> resMap = motorUtils.cal(dataShbModifyReqDTO.getPjdy(), dataShbModifyReqDTO.getPjdl(), dataShbModifyReqDTO.getGlysh(), dataShb.getCdxl(), dataShb.getMotorEddy(), dataShb.getMotorEddl(), dataShb.getMotorEdgl(), dataShb.getMotorEdxl(), dataShb.getMotorKzdl(), dataShb.getMotorKzgl(), dataShb.getMotorEdglys(), dataShb.getMotorJs(), dataShb.getMotorWgjjdl());
        dataShb.setYxzht(resMap.get("运行状态"));
        dataShb.setZhgl(resMap.get("轴功率"));
        dataShb.setDjxl(resMap.get("电机效率"));
        dataShb.setZhxl(String.valueOf(df2.format(Float.parseFloat(resMap.get("综合效率")) / 100f)));
        dataShb.setFzxsh(resMap.get("负载系数"));
        float edgl = Float.parseFloat(dataShb.getMotorEdgl());
        dataShb.setShcgl(String.format("%.2f", edgl * Float.parseFloat(dataShb.getFzxsh())));
        return dataShb;
    }
}
